home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / ASSEMBLE / 0938.ZIP / KEGELUNX.ARC / LINE1.ASM < prev    next >
Assembly Source File  |  1985-06-10  |  3KB  |  207 lines

  1. page 66, 132
  2. page
  3. ;---------- line?.com ------------
  4. ; Copies the [initial..final] lines of the named files to stdout.
  5. ; If no files named, uses stdout.
  6. ;
  7.  
  8. ; uses args
  9.     extrn    _args:near,    _shift:near
  10.     extrn    argc:word,    argv:word
  11.  
  12. ; uses ffind
  13.     extrn    ffirst:near,    fnext:near
  14.     extrn    fnam:byte
  15.  
  16. ; uses fio
  17.     extrn    f_io_init:near
  18.     extrn    f_ihand:word,    f_ohand:word
  19.     extrn    f_ibuf:word,    f_obuf:word
  20.     extrn    f_ilen:word,    f_olen:word
  21.     extrn    f_ibufend:word,    f_obufend:word
  22.     extrn    f_imore:near,    f_omore:near
  23.     extrn    f_getc:near,    f_putc:near
  24.  
  25. dos    macro    fn
  26.     mov    ah, fn
  27.     int    21h
  28.     endm
  29.  
  30. stdin    equ    0
  31. stdout    equ    1
  32. stderr    equ    2
  33.  
  34. ; Set the range of lines to print; 0 is the first line.
  35. initial    equ    0
  36. final    equ    0
  37.  
  38. code    segment para public 'CODE'
  39. assume cs:code,ds:code
  40.  
  41.     org    100h
  42. main    proc
  43.     jmp    linex
  44. main    endp
  45.  
  46.  
  47. ;----------------------------------------------
  48.  
  49.     db    27
  50.     db    '[2J Line1 (C) 1984 Yoyodyne, Inc- a growing excited company', 26
  51.  
  52. errs        dw    null        ; error zero- no error
  53.         dw    null, filenotfound, pathnotfound, nohand, access
  54.         dw    null, null, null, null, null
  55.         dw    null, null, null, null, baddrive
  56.         dw    null, null
  57.  
  58. null        db    '?Dos err', 0
  59. filenotfound    db    'File not found', 0
  60. pathnotfound    db    'Path not found', 0
  61. nohand        db    'No handles left', 0
  62. access        db    'Access denied', 0
  63. baddrive    db    'Invalid drive specification', 0
  64.  
  65. lfs        dw    0
  66. trueargc    dw    ?
  67.  
  68. ;--- strlen ---
  69. ; Returns length of ES:DI in CX, points to null with DI.
  70.  
  71. strlen    proc    near
  72.     cld
  73.     push    ax
  74.     mov    cx, -1
  75.     mov    al, 0
  76.     repnz    scasb
  77.     not    cx
  78.     pop    ax
  79.     ret
  80. strlen    endp
  81.  
  82. dos_err    proc    near
  83.     mov    bx, ax
  84.     add    bx, ax
  85.     mov    di, errs[bx]
  86.     push    di
  87.     call    strlen
  88.     pop    dx
  89.     mov    bx, stderr
  90.     DOS    40h
  91.     ret
  92. dos_err    endp
  93.  
  94. open_err    proc    near
  95.     call    dos_err
  96.     mov    al, 1
  97.     DOS    4ch
  98. open_err    endp
  99.  
  100. ;-------------------------------------------------
  101.  
  102. linex    proc    near
  103.  
  104.     call    _args
  105.  
  106.     ; Initialize the buffers...
  107.     mov    f_ilen, 512
  108.     mov    f_olen, 512
  109.     call    f_io_init
  110.  
  111.     ; Get them args...
  112.     mov    ax, argc
  113.     mov    trueargc, ax
  114.     or    ax, ax
  115.     jnz    while_loop
  116.         ; If no arguments, use stdin.
  117.         mov    argc, 1
  118.         mov    f_ihand, stdin
  119.         jmp    short gothandle
  120.  
  121.     ;----- Main Loop -------
  122.     ; do {
  123.     ;   find_first_matching(argv[2]);
  124.     ;   repeat
  125.     ;       line2;
  126.     ;       until not find_next;
  127.     ;   shift;
  128.     ;   } while (argc > 0);
  129.  
  130. while_loop:
  131.     mov    dx, argv[2]
  132.     mov    cx, 0
  133.     call    ffirst
  134.     jnc    rept_loop
  135.     jmp    while_done
  136.  
  137. rept_loop:
  138.     mov    dx, offset fnam
  139.     mov    al, 0        ; open for read access
  140.     dos    3dh
  141.     jc    open_err
  142.  
  143. gothandle:
  144.     mov    f_ihand, ax
  145.     mov    f_ohand, stdout
  146.  
  147.     ; SI & DI are used by getc & putc.
  148.     mov    si, f_ibuf
  149.     mov    f_ibufend, si
  150.     mov    di, f_obuf
  151.     mov    lfs, 0        ; initialize CRLF counter.
  152.  
  153. moreclp:
  154.     call    f_getc
  155.     jz    morecx
  156.     cmp    al, 26
  157.     jz    morecx    
  158.     cmp    lfs, initial
  159.     jb    not_yet
  160.         call    f_putc
  161. not_yet:
  162.     cmp    al, 10
  163.     jnz    moreclp
  164.     inc    lfs
  165.     cmp    lfs, final
  166.     jbe    moreclp
  167.  
  168. morecx:
  169.     call    f_omore        ; flush output buffer
  170.  
  171.     mov    bx, f_ihand
  172.     DOS    3Eh        ; close input file
  173.  
  174.     ;       until (not find_next);
  175.     cmp    trueargc, 0
  176.     jz    while_done
  177.     call    fnext
  178.     jc    rept_done
  179.     jmp    rept_loop
  180. rept_done:
  181.     ;   shift;
  182.     call    _shift
  183.     dec    argc
  184.     cmp    argc, 0
  185.     jz    while_done
  186.     jmp    while_loop
  187.  
  188. while_done:
  189.     mov    di, f_obuf    ; put an EOF, please (DOS won't).
  190.     mov    al, 26
  191.     call    f_putc
  192.     call    f_omore
  193.     mov    al, 0        ; no error
  194. exit:
  195.     mov    ah, 4ch
  196.     int    21h        ; terminate process, return status in AL.
  197.  
  198. linex    endp
  199.  
  200. code    ends
  201.  
  202.     end    main
  203.  
  204.  
  205.  
  206.  
  207.